package com.kh.samr.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * 实体类基类
 *
 * @author Harry
 * @date 2020/11/30
 */
@MappedSuperclass
@Data
public class BaseEntity implements Serializable {
	
	private static final long serialVersionUID = -5202965760276116939L;
	
	/**
	 * 主键
	 */
	@ApiModelProperty(value = "主键", readOnly = true)
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	@Id
	@GenericGenerator(name = "system-uuid", strategy = "uuid")
	@GeneratedValue(generator = "system-uuid")
	@Column(length = 32)
	private String id;
	
	/**
	 * 创建时间
	 */
	@ApiModelProperty(value = "创建时间", readOnly = true, example = "yyyy-MM-dd HH:mm:ss")
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false)
	private Date creatTime;
	
	/**
	 * 创建人
	 */
	@ApiModelProperty(value = "创建人", readOnly = true)
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	@Column(length = 32)
	private String creater;
	
	/**
	 * 最近修改时间
	 */
	@ApiModelProperty(value = "最近修改时间", readOnly = true, example = "yyyy-MM-dd HH:mm:ss")
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@Temporal(TemporalType.TIMESTAMP)
	private Date updateTime;
	
	/**
	 * 最近修改人
	 */
	@ApiModelProperty(value = "最近修改人", readOnly = true)
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	@Column(length = 32)
	private String updater;
	
	@PrePersist
	private void entityBeforeInsert() {
		setCreatTime(new Date());
		setUpdateTime(new Date());
	}
	
	@PreUpdate
	private void entityBeforeModify() {
		setUpdateTime(new Date());
	}
}
